/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.handler;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.game.room.Game;
import extension.room.phom.PhomExtension;
import extension.room.phom.PhomGame;
import extension.room.phom.PhomPlayer;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class EatCardHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        Debug.d("handle eat card");
        Debug.d(params.getDump());
        PhomExtension phomExtension = (PhomExtension) getParentExtension();
        PhomGame game = phomExtension.getGame();
        PhomPlayer p = game.getPlayerFight();
        if (game.getCanEat() == 1 && p.getName().equals(user.getName())) {
            game.eatCard();
            int canU = 0;
            p.generateListPhom();
            List<Integer> listPhom = p.getListPhom();
            if (listPhom.size() >= 9) {
                canU = 1;
            }
            Debug.i(user.getName() + "- eat card :" + game.getCardFight());
            List<User> listSend = phomExtension.getParentRoom().getUserList();
            listSend.remove(user);
            phomExtension.getResponse().sendEatCard(game.getCardFight(), game.getLastFight(), game.getGameTurn(), canU, listPhom, user);
            phomExtension.getResponse().sendEatCard(game.getCardFight(), game.getLastFight(), game.getGameTurn(), canU, listSend);
            if (canU == 1) {
                final PhomGame gameFinal = game;
                final PhomExtension extFinal = phomExtension;
                final PhomPlayer pFinal = p;
                final User uFinal = user;
                Thread delayThread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        long startTime = System.currentTimeMillis();
                        while (System.currentTimeMillis() - startTime < 3000) {
                        }
                        gameFinal.onFinishU(PhomGame.HAVE_PHOM);
                        if (pFinal.getNumberCardFights() >= gameFinal.getNumberCardFinish() - 1 && gameFinal.getIsPlay()) {
                            pFinal.clearPhomFinal();
                            gameFinal.setLastShowPhom();
                            if (!pFinal.getIsMoreFightTurn()) {
                                extFinal.getResponse().showPhom(pFinal.getHavePhom(), pFinal.getListPhom(), pFinal.getSeatPos(),
                                        uFinal);
                            } else {
                                extFinal.getResponse().showPhomMore(pFinal.getHavePhom(), pFinal.getListPhomMore(), pFinal.getSeatPos(),
                                        uFinal);
                            }
                        }

                    }
                });
                delayThread.start();
            } else {
                if (p.getNumberCardFights() >= game.getNumberCardFinish() - 1 && game.getIsPlay()) {
                    p.clearPhomFinal();
                    game.setLastShowPhom();
                    if (!p.getIsMoreFightTurn()) {
                        phomExtension.getResponse().showPhom(p.getHavePhom(), p.getListPhom(), p.getSeatPos(),
                                user);
                    } else {
                        phomExtension.getResponse().showPhomMore(p.getHavePhom(), p.getListPhomMore(), p.getSeatPos(),
                                user);
                    }
                }
            }
        }
    }

    private int checkWin(PhomGame game) {
        PhomPlayer p = game.getPlayerFight();
        p.generateListPhom();
        List<Integer> listPhom = p.getListPhom();
        int canU = 0;
        Debug.d("list phom size :" + listPhom.size());
        if (listPhom.size() >= 9) {
            canU = 1;
        }
        return canU;
    }
}
